import 'package:flutter_taotaoshipin/base/base_refresh_controller.dart';
import 'package:flutter_taotaoshipin/http/dao/drama_dao.dart';
import 'package:flutter_taotaoshipin/http/dao/interact_dao.dart';
import 'package:flutter_taotaoshipin/http/dao/user_dao.dart';
import 'package:flutter_taotaoshipin/model/interact_user_collect_entity.dart';
import 'package:flutter_taotaoshipin/model/interact_user_collect_rm_entity.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'ranking_state.dart';

class RankingController extends BaseRefreshController {
  final RankingState state = RankingState();

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  @override
  fetchData({bool showLoading = true}) async {
    // 开始加载
    if (showLoading) {
      loadStatus = 0;
      update();
    }

    try {
      // 加载记录
      var response1 = await DramaDao.getDramaRanking();
      state.rankingList = response1.data!;

      // 加载追剧记录
      var response4 = await UserDao.dramaCollect();
      state.collectList = response4.data!;

      if (showLoading) {
        loadStatus = 1;
      }
    } catch (e) {
      if (showLoading) {
        loadStatus = 3;
      } else {
        EasyLoading.showError('加载失败');
      }
    }
    update();
    // 结束下拉
    refreshController.refreshCompleted();
  }

  // 收藏
  void userCollectBtnClicked(int dramaId, bool isFollowing) async {
    // 先获取分集信息
    var result = await DramaDao.getDramaList(dramaId);
    if (result.data != null) {
      var model = result.data!.first;
      if (isFollowing) {
        // 取消收藏
        await loadCollectRmData(dramaId, model.sort!, model.id!);
      } else {
        // 收藏
        await loadCollectData(dramaId, model.sort!, model.id!);
      }
      // 加载追剧记录
      var response4 = await UserDao.dramaCollect();
      state.collectList = response4.data!;
      update();
    }
  }

  /// 收藏
  Future<InteractUserCollectEntity?> loadCollectData(
      int dramaId,
      int sort,
      int listId,
      ) async {
    InteractUserCollectEntity? entity;
    try {
      var result = await InteractDao.userCollect(
          dramaId: dramaId, sort: sort, listId: listId);
      if (result.code == 0) {
        entity = result.data;
      } else {
        if (result.message.isNotEmpty) {
          EasyLoading.showToast(result.message);
        }
      }
    } catch (_) {
      EasyLoading.showError('网络错误');
    }
    return entity;
  }

  /// 取消收藏
  Future<InteractUserCollectRmEntity?> loadCollectRmData(
      int dramaId,
      int sort,
      int listId,
      ) async {
    InteractUserCollectRmEntity? entity;
    try {
      var result = await InteractDao.userCollectRm(
          dramaId: dramaId, sort: sort, listId: listId);
      if (result.code == 0) {
        entity = result.data;
      } else {
        if (result.message.isNotEmpty) {
          EasyLoading.showToast(result.message);
        }
      }
    } catch (_) {
      EasyLoading.showError('网络错误');
    }
    return entity;
  }
}
